package com.dji.djiapp.activity;

import static android.view.View.VISIBLE;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.dji.djiapp.MApplication;
import com.dji.djiapp.R;
import com.dji.djiapp.config.ModuleVerificationUtil;
import com.dji.djiapp.widget.VideoFeedView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import dji.common.error.DJIError;
import dji.common.flightcontroller.virtualstick.FlightControlData;
import dji.common.util.CommonCallbacks;
import dji.sdk.camera.VideoFeeder;

public class FlightControlActivity extends AppCompatActivity {

    private String TAG = FlightControlActivity.class.getName();

    @BindView(R.id.control_left_up)
    Button controlLeftUpBtn;
    @BindView(R.id.control_left_down)
    Button controlLeftDownBtn;
    @BindView(R.id.control_left_left)
    Button controlLeftLeftBtn;
    @BindView(R.id.control_left_right)
    Button controlLeftRightBtn;
    @BindView(R.id.control_right_up)
    Button controlRightUpBtn;
    @BindView(R.id.control_right_down)
    Button controlRightDownBtn;
    @BindView(R.id.control_right_left)
    Button controlRightLeftBtn;
    @BindView(R.id.control_right_right)
    Button controlRightRightBtn;

    @BindView(R.id.control_takeoff)
    Button controlTakeoffBtn;
    @BindView(R.id.control_landing)
    Button controlLandingBtn;

    private SendVirtualStickDataTask sendVirtualStickDataTask;
    private Timer sendVirtualStickDataTimer;

    private float pitch;
    private float roll;
    private float yaw;
    private float throttle;

    private VideoFeedView fpvVideoFeedView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flight_control);
        ButterKnife.bind(this);

        fpvVideoFeedView = (VideoFeedView) findViewById(R.id.fpv_view);
        fpvVideoFeedView.registerLiveVideo(VideoFeeder.getInstance().getSecondaryVideoFeed(), false);
        if (ModuleVerificationUtil.isMultiStreamPlatform()){
            fpvVideoFeedView.setVisibility(VISIBLE);
        }

        initLeftOnClickListener();
        initRightOnClickListener();
    }

    private void initLeftOnClickListener() {

        controlTakeoffBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MApplication.getAircraftInstance()
                        .getFlightController().startTakeoff(new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onResult(DJIError djiError) {

                    }
                });
            }
        });

        controlLeftUpBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    throttle = 2F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    throttle = 0f;
                }
                return false;
            }
        });

        controlLeftDownBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    throttle = -2F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    throttle = 0f;
                }
                return false;
            }
        });
        controlLeftLeftBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    yaw = -50F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    yaw = 0f;
                }
                return false;
            }
        });
        controlLeftRightBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    yaw = 50F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    yaw = 0f;
                }
                return false;
            }
        });
    }

    private void initRightOnClickListener() {

        controlLandingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MApplication.getAircraftInstance()
                        .getFlightController().startLanding(new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onResult(DJIError djiError) {

                    }
                });
            }
        });

        controlRightUpBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    roll = 7.5F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    roll = 0f;
                }
                return false;
            }
        });

        controlRightDownBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    roll = -7.5F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    roll = 0f;
                }
                return false;
            }
        });

        controlRightLeftBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    pitch = -7.5F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    pitch = 0f;
                }
                return false;
            }
        });

        controlRightRightBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    pitch = 7.5F;
                    if (null == sendVirtualStickDataTimer) {
                        sendVirtualStickDataTask = new SendVirtualStickDataTask();
                        sendVirtualStickDataTimer = new Timer();
                        sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    pitch = 0f;
                }
                return false;
            }
        });
    }

    private class SendVirtualStickDataTask extends TimerTask {
        @Override
        public void run() {
            if (ModuleVerificationUtil.isFlightControllerAvailable()) {
                MApplication.getAircraftInstance()
                        .getFlightController()
                        .sendVirtualStickFlightControlData(new FlightControlData(pitch, roll, yaw, throttle),
                                new CommonCallbacks.CompletionCallback() {
                                    @Override
                                    public void onResult(DJIError djiError) {
//                                        Log.w(TAG, djiError.getDescription());
                                    }
                                });
            }
        }
    }
}